home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1997 / HAM Radio 1997.iso / vcls / date / examples / exinidb0.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-04-08  |  5.0 KB  |  209 lines

  1. unit Exinidb0;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, AdIniDB, Buttons, AdMisc, AdExcept;
  8.  
  9. type
  10.   TDataRecord = record
  11.     Name : String[21];
  12.     Age  : Integer;
  13.   end;
  14.  
  15. type
  16.   TForm1 = class(TForm)
  17.     Records: TGroupBox;
  18.     ListBox1: TListBox;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Edit1: TEdit;
  22.     Edit2: TEdit;
  23.     BitBtn1: TBitBtn;
  24.     BitBtn2: TBitBtn;
  25.     BitBtn3: TBitBtn;
  26.     IniDBase1: TApdIniDBase;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure BitBtn1Click(Sender: TObject);
  29.     procedure BitBtn3Click(Sender: TObject);
  30.     procedure ListBox1Click(Sender: TObject);
  31.     procedure BitBtn2Click(Sender: TObject);
  32.   private
  33.     { Private declarations }
  34.   public
  35.     { Public declarations }
  36.     procedure ValidateEntry(var Rec : TDataRecord);
  37.       {-Make sure everything entered in the TEdit's is valid}
  38.     procedure UpdateEdits;
  39.       {-Update the data in the TEdits}
  40.   end;
  41.  
  42. var
  43.   Form1: TForm1;
  44.  
  45. implementation
  46.  
  47. {$R *.DFM}
  48.  
  49. procedure TForm1.ValidateEntry(var Rec : TDataRecord);
  50.   {-Make sure everything entered in the TEdit's is valid}
  51. var
  52.   E       : Integer;
  53.   NameStr : String;
  54.   AgeStr  : String;
  55.  
  56. begin
  57.   {remove trailing spaces from input data}
  58.   NameStr := TrimTrail(Edit1.Text);
  59.   AgeStr  := TrimTrail(Edit2.Text);
  60.  
  61.   {make sure the user entered a name}
  62.   if (NameStr = '') then begin
  63.     Edit1.SetFocus;
  64.     raise Exception.Create('You must enter a name');
  65.   end;
  66.  
  67.   {make sure the user entered an age}
  68.   if (AgeStr = '') then begin
  69.     Edit2.SetFocus;
  70.     raise Exception.Create('You must enter an age');
  71.   end;
  72.  
  73.   {validate the entered age--make sure it's really a number}
  74.   Val(Edit2.Text, Rec.Age, E);
  75.   if (E <> 0) then begin
  76.     Edit2.SetFocus;
  77.     raise Exception.Create('Invalid age');
  78.   end;
  79.  
  80.   Rec.Name := Edit1.Text;
  81. end;
  82.  
  83. procedure TForm1.UpdateEdits;
  84.   {-Update the data in the TEdits}
  85. var
  86.   Rec : TDataRecord;
  87.  
  88. begin
  89.   if (ListBox1.ItemIndex <> -1) then begin
  90.     {read the selected record from the database}
  91.     IniDBase1.GetRecord(ListBox1.Items[ListBox1.ItemIndex], Rec);
  92.  
  93.     {update the TEdits on the form with the new data}
  94.     Edit1.Text := Rec.Name;
  95.     Edit2.Text := IntToStr(Rec.Age);
  96.   end else begin
  97.     {blank the TEdits}
  98.     Edit1.Text := '';
  99.     Edit2.Text := '';
  100.   end;
  101. end;
  102.  
  103. procedure TForm1.FormCreate(Sender: TObject);
  104. begin
  105.   {put the initial list of records in the listbox}
  106.   Listbox1.Items := IniDBase1.RecordList;
  107.  
  108.   {select the first item in the listbox. if there are any records available}
  109.   if (ListBox1.Items.Count <> 0) then begin
  110.     ListBox1.ItemIndex := 0;
  111.     UpdateEdits;
  112.   end;
  113. end;
  114.  
  115. procedure TForm1.BitBtn1Click(Sender: TObject);
  116. var
  117.   Rec : TDataRecord;
  118.  
  119. begin
  120.   {make sure the user has enetered valid data}
  121.   ValidateEntry(Rec);
  122.  
  123.   {add the record to the database}
  124.   try
  125.     {put the record in the file}
  126.     IniDBase1.AddRecord(Rec);
  127.  
  128.     {put the new record's name in the listbox and highlight it}
  129.     ListBox1.Items.Add(Rec.Name);
  130.     ListBox1.ItemIndex := Pred(ListBox1.Items.Count);
  131.   except
  132.     {if the record already exists, let the user know}
  133.     on ERecordExists do
  134.       MessageDlg('A record with that name already exists', mtError, [mbOK], 0);
  135.     else
  136.       raise;
  137.   end;
  138. end;
  139.  
  140. procedure TForm1.BitBtn2Click(Sender: TObject);
  141. var
  142.   Idx : Integer;
  143.   Rec : TDataRecord;
  144.  
  145. begin
  146.   {make sure something in the listbox is selected}
  147.   Idx := ListBox1.ItemIndex;
  148.   if (Idx = -1) then begin
  149.     MessageBeep(0);
  150.     Exit;
  151.   end;
  152.  
  153.   {get input data}
  154.   ValidateEntry(Rec);
  155.  
  156.   {change the record in the file}
  157.   IniDBase1.UpdRecord(ListBox1.Items[Idx], Rec);
  158.  
  159.   {change the text in the listbox, if necessary, and re-highlight the record}
  160.   ListBox1.Items[Idx] := Rec.Name;
  161.   ListBox1.ItemIndex  := Idx;
  162. end;
  163.  
  164. procedure TForm1.BitBtn3Click(Sender: TObject);
  165. var
  166.   Idx    : Integer;
  167.   NewIdx : Integer;
  168.   Rec    : TDataRecord;
  169.  
  170. begin
  171.   {make sure something in the listbox is selected}
  172.   Idx := ListBox1.ItemIndex;
  173.   if (Idx = -1) then begin
  174.     MessageBeep(0);
  175.     Exit;
  176.   end;
  177.  
  178.   {make sure the user really wants to delete the record}
  179.   IniDBase1.GetRecord(ListBox1.Items[Idx], Rec);
  180.   if (MessageDlg('Are you sure you want to delete "' + Rec.Name + '"?',
  181.                   mtConfirmation, [mbYes, mbNo], 0) = mrYes) then begin
  182.     {remove the record from the file}
  183.     IniDBase1.DelRecord(ListBox1.Items[Idx]);
  184.  
  185.     {calculate the new position for the highlight bar in the listbox}
  186.     NewIdx := -1;
  187.     if (Idx = Pred(ListBox1.Items.Count)) then begin
  188.       if (Idx <> 0) then
  189.         NewIdx := Pred(Idx);
  190.     end else
  191.       NewIdx := Idx;
  192.  
  193.     {delete the record from the listbox and highlight another record}
  194.     ListBox1.Items.Delete(Idx);
  195.     ListBox1.ItemIndex := NewIdx;
  196.  
  197.     {update the edit components on the form}
  198.     UpdateEdits;
  199.   end;
  200. end;
  201.  
  202. procedure TForm1.ListBox1Click(Sender: TObject);
  203. begin
  204.   UpdateEdits;
  205. end;
  206.  
  207. end.
  208.  
  209.